package com.wisdudu.blank.support.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by pc on 2016/4/13.
 */
public class NumberView extends View {


    int[] x;
    List<String> listDate;                    //如果是月的话是显示的数据源过滤后的;
    List<String> allListDate;                //所有的,
    List<Integer> listWeigth = new ArrayList<>();
    public int paddingLeft;
    public int paddingRight;
    public int paddingBottom;
    public int paddintTop;
    int jgwidth;

    int index = -1;

    MyLineGraphView view;

    public void setMyLineGraphView(MyLineGraphView view) {
        this.view = view;
    }


    public void setInit(List<String> listDate, List<String> allListDate, MyLineGraphView view) {
        this.listDate = listDate;
        this.allListDate = allListDate;
        this.view = view;
        Log.e("-->xxx1::", listDate.size() + "");
        Log.e("-->xxx2::", allListDate.size() + "");
        postInvalidate();
    }


    public void setIndex(int index){
        this.index=index;
        postInvalidate();
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

//        listDate = new ArrayList<>();
//        for (int i = 1; i <= 10; i++) {
//            listDate.add(i+"");
//        }


        try {
            paddingLeft = 30;
            paddingRight = 30;
            if (index == -1) {
                index = listDate.size() - 1;
            }
            jgwidth = (getWidth() - paddingLeft - paddingRight) / (listDate.size() - 1);
            x = new int[listDate.size()];
            for (int i = 0; i < listDate.size(); i++) {
                double kuan = getMyWeight(i);
                x[i] = (int) kuan;
            }

        } catch (Exception ex) {

        }


        /**
         * 画圆外部蓝色
         */
        Paint paint = new Paint();
        //设置画笔颜色
        paint.setColor(Color.parseColor("#d2d2d2"));
        //设置圆弧的宽度
        paint.setStrokeWidth(4);
        //消除锯齿
        paint.setAntiAlias(true);
        canvas.drawLine(0, 0, getWidth(), 0, paint);
        canvas.drawLine(0, getHeight(), getWidth(), getHeight(), paint);


        int width;
        //绘制文字
        paint = new Paint();
        paint.setFakeBoldText(true); //true为粗体，false为非粗体
        //mp.setTextSkewX(-0.5f); //float类型参数，负数表示右斜，整数左斜
        //mp.setUnderlineText(true); //true为下划线，false为非下划线
        //mp.setStrikeThruText(true); //true为删除线，false为非删除线
        paint.setTextSize(28); //设置字体大小，int型，如12
        paint.setShader(null);
        paint.setStrokeWidth(10);
        paint.setAntiAlias(true);
        try {
            for (int i = 0; i < x.length; i++) {
                width = getStringWidth(listDate.get(i) + "", paint);
                canvas.drawText(listDate.get(i) + "", x[i] - width / 2, getHeight() / 2 + 10, paint);
                listWeigth.add(width);
            }
        } catch (Exception e) {

        }

        //绘制文字背景
        paint = new Paint();
        paint.setAntiAlias(true);                       //设置画笔为无锯齿
        paint.setColor(Color.parseColor("#e9ac43"));
        paint.setStrokeWidth((float) 1.5);              //线宽
        paint.setStyle(Paint.Style.FILL);                   //空心效果
        paint.setAntiAlias(true);

        try {
            width = listWeigth.get(index);
            RectF r1 = new RectF(x[index] - width / 2 - 5, getHeight() / 2 - 20, x[index] - width / 2 + width + 5, getHeight() / 2 + 20);
            canvas.drawRoundRect(r1, 10, 10, paint);
        } catch (Exception e) {

        }


        paint = new Paint();
        paint.setFakeBoldText(true); //true为粗体，false为非粗体
        paint.setTextSize(28); //设置字体大小，int型，如12
        paint.setShader(null);
        paint.setStrokeWidth(10);
        paint.setAntiAlias(true);
        paint.setColor(Color.parseColor("#FFFFFF"));
        try {
            width = getStringWidth(listDate.get(index) + "", paint);
            canvas.drawText(listDate.get(index) + "", x[index] - width / 2, getHeight() / 2 + 10, paint);
        }catch (Exception e){

        }

    }

    private int getStringWidth(String str, Paint paint) {
        return (int) paint.measureText(str);
    }


    public NumberView(Context context) {
        super(context);
    }

    public NumberView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public NumberView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    public double getMyWeight(int posotion) {
        return posotion * jgwidth + paddingLeft;
    }


    boolean pd1 = false;
    boolean pd2 = false;
    float mx1, mx2, my1, my2;

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:  //按下 = 0
                mx1 = event.getX();
                my1 = event.getY();
                if (pd1) {
                    pd1 = false;
                }
                pd1 = true;
                break;
            case MotionEvent.ACTION_MOVE:  //移动 = 2
                pd2 = true;
                break;
            case MotionEvent.ACTION_UP:    // 抬起 = 1
                mx2 = event.getX();
                my2 = event.getY();
                float number = mx1 - mx2 > 0 ? mx1 - mx2 : mx2 - mx1;
                if (pd1 & pd2 & number > 10) {
                    if (number > 10) {
                        // Log.e("-->","滑动");
                        // invalidate();
                    }
                } else {
                    int index = isok(event.getX(), event.getY());
                    if (index != -1) {
                        //有点击事件
                        //    Toast.makeText(myContext,"点击的第"+(index+1), Toast.LENGTH_SHORT).show();
                        Log.e("-->", "点击的下标:" + (index) + "");
                        Log.e("-->", "点击的值" + listDate.get(index) + "");
                        this.index = index;
                        for (int i = 0; i < allListDate.size(); i++) {
                            if (listDate.get(index).equals(allListDate.get(i))) {
                                Log.e("-->iiii:", i + "");
                                view.setBuleIndex(i);
                            }
                        }
                    }
                    invalidate();
                    Log.e("-->", "抬起");
                }
                break;
        }
        return true;
    }


    //判断点击的坐标是否在个圆圈里面,是则返回圈的下标,不是返回-1
    public int isok(float djx, float djy) {
        for (int i = 0; i < x.length; i++) {
            if (isXiangjiao(djx, 0, x[i] - 30, 0, x[i] + 30, 0)) {
                return i;
            }
        }
        return -1;
    }

    //判断点和矩形是否相交
    public boolean isXiangjiao(float dx, float dy, int juBeginX, int juBeginY, int juEndX, int juEndY) {
        if (dx >= juBeginX & dy >= juBeginY & dx <= juEndX & dy <= juEndY) {
            return true;
        } else {
            return false;
        }
    }


}
